package com.my.juc.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/3/5 19:44
 */

// 比较两个接口
// 实现Runnable接口
class MyThread1 implements Runnable{

    @Override
    public void run() {
        System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
    }
}
// 实现Callable接口 
class MyThread2 implements Callable{

    @Override
    public Integer call() throws Exception {
        return 200;
    }
}
public class Demo1 {

    public static void main(String[] args) {
        // Runnable接口创建线程
        new Thread(new MyThread1(), "AA").start();
        
        // Callable接口
        FutureTask<Integer> futureTask  = new FutureTask(new MyThread2());
        try {
            new Thread(futureTask, "lucy").start();
            while (!futureTask.isDone()){
                System.out.println("wait.........");
            }
            Integer res = futureTask.get();
            System.out.println("futureTask.isDone() = " + futureTask.isDone());
            System.out.println("res = " + res);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        System.out.println("Aaaaaaaaaa");

    }
    
}
